// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/init/init.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>

namespace kumo::pollux::functions::test {
namespace {
class ZipBenchmark : public functions::test::FunctionBenchmarkBase {
 public:
  ZipBenchmark() : FunctionBenchmarkBase() {
    prestosql::registerArrayFunctions();
  }

  VectorPtr makeData(vector_size_t arraySize, bool dictionaryEncoding) {
    constexpr vector_size_t size = 100;

    std::vector<std::optional<int64_t>> values;
    values.reserve(size * arraySize);
    for (int i = 0; i < size * arraySize; i++) {
      values.push_back(i);
    }

    VectorPtr valuesVector = vectorMaker_.dictionary_vector(values);

    std::vector<vector_size_t> offsets;
    offsets.reserve(size);
    for (int i = 0; i < size; i++) {
      offsets.push_back(i * arraySize);
    }

    VectorPtr array = vectorMaker_.array_vector(offsets, valuesVector);

    if (dictionaryEncoding) {
      BufferPtr indices = AlignedBuffer::allocate<vector_size_t>(size, pool());
      auto rawIndices = indices->asMutable<vector_size_t>();
      for (int i = 0; i < size; i++) {
        rawIndices[i] = i;
      }

      array = BaseVector::wrap_in_dictionary(nullptr, indices, size, array);
    }

    return array;
  }

  size_t run(const VectorPtr& c0, const VectorPtr& c1) {
    melon::BenchmarkSuspender suspender;
    auto inputs = vectorMaker_.row_vector({c0, c1});

    auto exprSet = compileExpression("zip(c0, c1)", inputs->type());
    suspender.dismiss();

    return doRun(exprSet, inputs);
  }

  size_t doRun(exec::ExprSet& exprSet, const RowVectorPtr& row_vector) {
    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      cnt += evaluate(exprSet, row_vector)->size();
    }
    melon::doNotOptimizeAway(cnt);
    return cnt;
  }
};

BENCHMARK_MULTI(EqSizeFlatFlat) {
  ZipBenchmark benchmark;
  melon::BenchmarkSuspender suspender;
  auto c0 = benchmark.makeData(5, false);
  auto c1 = benchmark.makeData(5, false);
  suspender.dismiss();

  return benchmark.run(c0, c1);
}

BENCHMARK_MULTI(EqSizeFlatDictionary) {
  ZipBenchmark benchmark;
  melon::BenchmarkSuspender suspender;
  auto c0 = benchmark.makeData(5, false);
  auto c1 = benchmark.makeData(5, true);
  suspender.dismiss();

  return benchmark.run(c0, c1);
}

BENCHMARK_MULTI(NeqSizeFlatFlat) {
  ZipBenchmark benchmark;
  melon::BenchmarkSuspender suspender;
  auto c0 = benchmark.makeData(5, false);
  auto c1 = benchmark.makeData(3, false);
  suspender.dismiss();

  return benchmark.run(c0, c1);
}
} // namespace
} // namespace kumo::pollux::functions::test

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};

  melon::runBenchmarks();
  return 0;
}
